/*
 * @Description:全局方法
 */
// import _ from 'lodash'
import dayjs from "dayjs";
import axios from "axios";
// import { useRoute } from "vue-router";

import storageHelper from "./StorageHelper";
import {$get_power} from "../utils/utils";

export default {
  install(app) {
    // let route = useRoute();
    // app.config.globalProperties.$_ = _

    app.config.globalProperties.$dateFilterYMDHMS = (date) => {
      if (date && date.length === 10) {
        return dayjs(date).format("YYYY-MM-DD");
      } else {
        return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
      }
    };

    app.config.globalProperties.$dateFilterYMD = (date) => {
      return dayjs(date).format("YYYY-MM-DD");
    };
    /**
     * 下载
     * @param {String} url 下载地址
     * @param {String} name 保存的名字
     */
    app.config.globalProperties.$download = (url, name) => {
      var anchor = document.createElement("a");
      if (!name) {
        var arr = url.split("/");
        name = arr[arr.length - 1];
      }
      if ("download" in anchor) {
        anchor.href = url.replace("~/", host);
        anchor.setAttribute("download", name);
        anchor.className = "download-js-link";
        anchor.style.display = "none";
        document.body.appendChild(anchor);
        setTimeout(function () {
          anchor.click();
          document.body.removeChild(anchor);
        }, 66);
        return true;
      }
    };

 
    /**
     * 跳转地址
     * @param {String} url
     */
    app.config.globalProperties.$redirect = (url) => {
      if (url) {
        redirect_url = url;
      } else {
        return redirect_url;
      }
    };

    /**
     * @description 过滤数组
     * @param {Array} arr 被过滤的数组
     * @param {String} key 判断的键
     * @param {Object} value 判断的值
     * @return {Array} 返回过滤后的数组
     */
    app.config.globalProperties.$filter = (arr, key, value) => {
      var ar = [];
      for (var i = 0; i < arr.length; i++) {
        var o = arr[i];
        if (o[key] === value) {
          ar.push(o);
        }
      }
      return ar;
    };

    /**
     * @description 转url字符串
     * @param {Object} obj 被转换的对象
     * @param {String} url 请求地址
     * @return {String} url参数格式字符串
     */
    app.config.globalProperties.$toUrl = (obj, url) => {
      var queryStr = "";
      for (var key in obj) {
        var value = obj[key];
        if (typeof value === "number") {
          if (value > 0) {
            queryStr += "&" + key + "=" + obj[key];
          }
        } else if (value) {
          queryStr += "&" + key + "=" + encodeURI(value);
        }
      }
      if (url) {
        if (url.endWith("?") || url.endWith("&")) {
          return url + queryStr.replace("&", "");
        } else if (url.indexOf("?") === -1) {
          return url + queryStr.replace("&", "?");
        } else {
          return url + queryStr;
        }
      } else {
        return queryStr.replace("&", "");
      }
    };

    /**
     * 转换名称
     * @param {Array} list 数组
     */
    app.config.globalProperties.$toName = (
      list,
      value,
      value_key = "name",
      key = "value"
    ) => {
      var ret = "";
      for (var i = 0; i < list.length; i++) {
        var o = list[i];
        if (o[key] === value) {
          ret = o[value_key];
        }
      }
      return ret;
    };

    /**
     * 补全请求url
     * @param {String} url 现地址
     * @return {String} 新地址
     */
    app.config.globalProperties.$fullUrl = (url) => {
      var url_new = "";
      if (url) {
        if (url.indexOf("/http://") === 0) {
          url_new = url.replace("/http://", "http://");
        } else if (url.indexOf("~/") === 0) {
          url_new = url.replace("~/", host);
        } else if (url.indexOf("/") === 0) {
          url_new = url.replace("/", host);
        } else if (url.indexOf("~") === 0) {
          url_new = url.replace("~", "");
        } else {
          url_new = url;
        }
      }
      return url_new;
    };
    /**
     * 上传文件
     * @param {String} url 请求地址
     * @param {Object} body 请求参数
     * @param {Funciton} func 回调函数，可以为空，为空则采用await返回值
     * @return {Object} 返回请求结果
     */
    app.config.globalProperties.$upload = async (url, body, func) => {
      url = url.replace("~/", host);

      var token = this.$store.state.user.token || $.db.get("token") || null;

      if (func) {
        // 如果回调函数存在, 则采用异步
        this.$axios
          .post(url, body, {
            headers: {
              "x-auth-token": token,
              "Content-Type": "multipart/form-data",
            },
          })
          .then((res) => {
            func(res.data);
          })
          .catch((res) => {
            func(res);
          });
      } else {
        // 否则采用同步
        var res = await this.$axios.post(url, body, {
          headers: {
            "x-auth-token": token,
            "Content-Type": "multipart/form-data",
          },
        });
        return res.data;
      }
    };
    app.config.globalProperties.$check_comment = (path) => {
      var o = $get_power(path);
      if (o) {
        var option = JSON.parse(o.option);
        if (option.can_show_comment) return true;
      }
      return false;
    };
    app.config.globalProperties.$check_option = (path, op) => {
      let o = $get_power(path);
      if (o) {
        let option = JSON.parse(o.option);
        if (option[op]) return true;
      }
      return false;
    };
   
    // 报表打印
    app.config.globalProperties.$reportPrint = (
      params,
      statementUrl,
      statementName
    ) => {
      let account = "OLM";
      let password = "OLM123456";
      let url =
        "/webroot/decision/login/cross/domain" +
        "?fine_username=" +
        encodeURIComponent(account) +
        "&fine_password=" +
        encodeURIComponent(password) +
        "&validity=" +
        -1;
      if (storageHelper.getTabSystemTitle() === "制造中心") {
        if (!params.base_id) params["base_id"] = storageHelper.getBaseValue();
      }
      axios.get(url).then((res) => {
        if (res.status === 200) {
          FR.doHyperlinkByPost({
            //报表路径
            url: statementUrl,
            //参数
            para: params,
            target: "_blank", //对话框方式打开
            feature: {
              width: 700,
              height: 500,
              title: statementName,
            },
          });
        }
      });
    };
  },
};
